package sgg.algorithm.datastructure.sort;

import java.util.Arrays;

/**
 * @author: Michael
 * @date: 2025/4/30
 * @desc: 选择排序
 */
public class SelectionSort {

    public static void main(String[] args) {
        int arr[] = {101, 34, 119, 1};
        System.out.println(Arrays.toString(selectionSort(arr)));
    }

    public static int[] selectionSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            if (minIndex != i) {
                int temp = arr[i];
                arr[i] = arr[minIndex];
                arr[minIndex] = temp;
            }
        }
        return arr;
    }
}
